<template src="./template.html"></template>

<script>
/* global moment */
import apiUrls from 'common/api-urls'
import ResizeMixin from 'mixins/resize'
import { PAGE_CONFIG } from 'common/consts';
import { COL_INDEXS, FILTER_PARAMS, EXPIRED_STATUS, EXPIRED_STATUS_SIMPLIFY } from './consts';
import { getCustomerPage, deleteCustomer } from './models';

export default {
  mixins: [ResizeMixin],
  data() {
    return {
      enUrl: apiUrls.enUrl, // 地址
      COL_INDEXS,
      FILTER_PARAMS,
      EXPIRED_STATUS,
      EXPIRED_STATUS_SIMPLIFY,
      isPageLoading: false,

      tableList: [],
      PAGE_CONFIG: PAGE_CONFIG,
      pageNo: PAGE_CONFIG.defaultPageNo,
      pageSize: PAGE_CONFIG.defaultPageSize,
      totalCount: 0,
      filterForm: {
        [FILTER_PARAMS.orgTypeCode.interfaceParam]: '',
        [FILTER_PARAMS.expiredStatus.interfaceParam]: ''
      },
    };
  },
  computed: {
    userInfo() {
      return this.$store.state.user.userInfo
    }
  },

  methods: {
    // 删除请求
    async $handleDeleteHttp(params) {
      try {
        this.isLoading = true
        await deleteCustomer(params)
        this.$message.success('企业删除成功！')
        this.$search()
      } catch (err) {
        this.isLoading = false
        this.$message.error(err.retDesc || err.message || '删除出错')
      }
    },
    // 删除点击
    $handleDelete(row) {
      // const params = {
      //   ids: [row.customerId]
      // }
      const params = [row.customerId]
      this.$confirm('你确定要删除该企业吗？', {
        type: 'warning',
        confirmButtonText: '删除',
      }).then(() => {
        this.$handleDeleteHttp(params)
      }).catch(() => {
        console.log('取消');
      });
    },
    $search() {
      this.pageNo = 1
      this.$getTableList()
    },
    $handleAdd() {
      this.$router.push('/base/org/add')
    },
    $handleEdit(row) {
      this.$router.push(`/base/org/edit/${row.customerId}`)
    },
    $handleDetail(row) {
      this.$router.push(`/base/org/detail/${row.customerId}`)
    },
    $loginEnterprise(row) {
      window.open(`${this.enUrl}?customerId=${row.customerId}`, '_blank')
      // this.$router.push(`/base/org/login/${row.customerId}`)
    },
    async $getTableList() {
      this.isPageLoading = true;
      const params = {
        ...this.filterForm,
        limit: this.pageSize,
        page: this.pageNo
      };
      try {
        const { data } = await getCustomerPage(params);
        this.tableList = (data.list || []).map(it => ({
          ...it,
          deadlineTime: it.deadlineTime && moment(it.deadlineTime).format('YYYY-MM-DD'),
          createTime: moment(it.createTime).format('YYYY-MM-DD HH:mm'),
        }));
        console.log(this.tableList)
        this.totalCount = data.total
        this.isPageLoading = false;
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || err.message || '获取机构列表出错')
      }
    },
    $handlePageChange() {
      this.$getTableList();
    },
  },
  created() {
    this.$getTableList();
  },
};
</script>
